package com.fushenlan.helper;

import com.fushenlan.po.PageInfoVo;
import com.github.pagehelper.PageInfo;

import java.util.ArrayList;
import java.util.List;

public class ListPageHelper {

    /**
     * @param page     当前页数
     * @param pageSize 每页得大小
     * @param list     分页的对象
     * @return
     */
    public static List<?> getListPage(int page, int pageSize, List<?> list) {
        if (list == null || list.size() == 0) {
            return list;
        }
        int totalCount = list.size();
        page = page - 1;
        int fromIndex = page * pageSize;
        //分页不能大于总数
        if (fromIndex >= totalCount) {
            throw new RuntimeException("页数或分页大小不正确!");
        }
        int toIndex = ((page + 1) * pageSize);
        if (toIndex > totalCount) {
            toIndex = totalCount;
        }
        return list.subList(fromIndex, toIndex);
    }

    public static PageInfoVo StartPage(PageInfo pageInfo){

        PageInfoVo response = new PageInfoVo();
         List list= new ArrayList();
            //获取数据列表
            list = pageInfo.getList();
            //获取数据总数
            response.setList(pageInfo.getList());
            response.setPageSize(pageInfo.getPageSize());
            response.setTotal(pageInfo.getTotal());
            response.setPageNo(pageInfo.getPageNum());
            response.setSize(pageInfo.getSize());
            return  response;
    }
}
